package com.kinon.norogamilk.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kinon.norogamilk.R;
import com.kinon.norogamilk.bean.BaseBean;
import com.kinon.norogamilk.bean.SpecBean;
import com.kinon.norogamilk.bean.SpecInfoBean;
import com.kinon.norogamilk.http.CommonSubscriber;
import com.kinon.norogamilk.http.MilkApi;
import com.kinon.norogamilk.ui.base.BaseActivity;
import com.kinon.norogamilk.util.RxUtil;

import java.io.Serializable;
import java.util.ArrayList;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;


/**
 * Created by lhqq on 2017-12-12.
 * 自主购买
 */

public class SelfBuyActivity extends BaseActivity implements View.OnClickListener {

    protected CompositeDisposable mCompositeDisposable;
    private Button btn_back;
    private Button btn_milk;
    private Button btn_yogurt;
    private LinearLayout rl_yogurt;
    private LinearLayout rl_milk;
    /**
     * 酸奶大容量
     */
    private TextView tv_yMaxCapacity;
    /**
     * 酸奶大容量价格
     */
    private TextView tv_yMaxPrice;
    /**
     * 酸奶小容量
     */
    private TextView tv_yMinCapacity;
    /**
     * 酸奶小容量价格
     */
    private TextView tv_yMinPrice;
    /**
     * 鲜奶大容量
     */
    private TextView tv_mMaxCapacity;
    /**
     * 鲜奶大容量价格
     */
    private TextView tv_mMaxPrice;
    /**
     * 鲜奶小容量
     */
    private TextView tv_mMinCapacity;
    /**
     * 鲜奶小容量价格
     */
    private TextView tv_mMinPrice;
    private ArrayList<SpecBean> specList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_selfbuy_layout);
        initView();
    }

    private void initView() {
        btn_back = findViewById(R.id.btn_back);
        btn_milk = findViewById(R.id.btn_milk);
        btn_yogurt = findViewById(R.id.btn_yogurt);
        rl_yogurt = findViewById(R.id.rl_yogurt);
        rl_milk = findViewById(R.id.rl_milk);
        tv_yMaxCapacity = findViewById(R.id.tv_yMaxCapacity);
        tv_yMaxPrice = findViewById(R.id.tv_yMaxPrice);
        tv_yMinCapacity = findViewById(R.id.tv_yMinCapacity);
        tv_yMinPrice = findViewById(R.id.tv_yMinPrice);
        tv_mMaxCapacity = findViewById(R.id.tv_mMaxCapacity);
        tv_mMaxPrice = findViewById(R.id.tv_mMaxPrice);
        tv_mMinCapacity = findViewById(R.id.tv_mMinCapacity);
        tv_mMinPrice = findViewById(R.id.tv_mMinPrice);

        btn_back.setOnClickListener(this);
        btn_milk.setOnClickListener(this);
        btn_yogurt.setOnClickListener(this);
        rl_milk.setOnClickListener(this);
        rl_yogurt.setOnClickListener(this);
        getGoods();
    }

    private void getGoods() {
        Disposable subscription = MilkApi.getInstance()
                .getGoods(sn)
                .compose(RxUtil.<BaseBean<ArrayList<SpecBean>>>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<BaseBean<ArrayList<SpecBean>>>() {
                    @Override
                    public void onNext(BaseBean<ArrayList<SpecBean>> baseBean) {
//                        baseBean.getData();
                        if (baseBean != null) {
                            setView(baseBean);
                        }

                    }

                    @Override
                    public void onFail(Throwable t, int code, String msg) {

                    }
                });
        mCompositeDisposable = new CompositeDisposable();
        mCompositeDisposable.add(subscription);
    }

    private void setView(BaseBean<ArrayList<SpecBean>> baseBean) {
        if (baseBean.getData() != null && baseBean.getData().size() > 0) {
            specList.addAll(baseBean.getData());
            try {
                tv_yMaxCapacity.setText(specList.get(0).getSpecs().get(0).getSpec());
                tv_yMaxPrice.setText("￥" + String.valueOf(specList.get(0).getSpecs().get(0).getPrice()));
                tv_yMinCapacity.setText(specList.get(0).getSpecs().get(1).getSpec());
                tv_yMinPrice.setText("￥" + String.valueOf(specList.get(0).getSpecs().get(1).getPrice()));
                tv_mMaxCapacity.setText(specList.get(1).getSpecs().get(0).getSpec());
                tv_mMaxPrice.setText("￥" + String.valueOf(specList.get(1).getSpecs().get(0).getPrice()));
                tv_mMinCapacity.setText(specList.get(1).getSpecs().get(1).getSpec());
                tv_mMinPrice.setText("￥" + String.valueOf(specList.get(1).getSpecs().get(1).getPrice()));

            } catch (Exception e) {
                e.printStackTrace();
            }
        }


//        Log.e("lh", "specList.get(0)== "+specList.get(0).toString() );
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_yogurt:
                if (specList.size() > 0) {
                    intent(0);
                }
                break;
            case R.id.btn_milk:
                if (specList.size() > 1) {
                    intent(1);
                }
//                specList.get(1);
                break;
            case R.id.rl_yogurt:
                if (specList.size() > 0) {
                    intent(0);
                }
                break;
            case R.id.rl_milk:
                if (specList.size() > 1) {
                    intent(1);
                }
                break;
            default:
                break;
        }
    }

    private void intent(int type) {
        Log.e("lh", "type== " + type);
        Log.e("lh", "getSpecs== " + specList.get(type).getSpecs().toString());
        ArrayList<SpecInfoBean> specS = new ArrayList<>();
        specS.addAll(specList.get(type).getSpecs());
        Intent intent = new Intent(SelfBuyActivity.this, SpecSelectActivity.class);
        intent.putExtra("specs", (Serializable) specS);
//        intent.putExtra(Constant.MILK_TYPE,type);
        startActivity(intent);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mCompositeDisposable != null) {
            mCompositeDisposable.clear();
        }
    }

}
